// app.js

App({
  globalData: {
    'appid': 'wx984d508ce8f82e87',
    'url': 'https://api.resource.temp.ziqiang.net.cn/',
    'token': '',
    'favorities': [],
    "isdeleted": false,
    system: ''
  },
  onLaunch() {
    // 登录
    let a = wx.getSystemInfoSync().system
    this.globalData.system = a  
  },

  get(route, obj) {
    return new Promise((resolve, reject) => {
      this.obj = obj || {}
      wx.request({
        url: this.globalData.url + route,
        method: 'GET',
        header: {
          'Authorization': this.globalData.token
        },
        data: obj,
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },

  post(route, obj) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.globalData.url + route,
        method: 'POST',
        header: {
          'Authorization': this.globalData.token,
          // 'content-type': 'multipart/form-data'
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: obj,
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },

  delete(route, obj) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.globalData.url + route,
        method: 'DELETE',
        header: {
          'Authorization': this.globalData.token,
          // 'content-type': 'multipart/form-data'
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: obj,
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },

  put(route, obj) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.globalData.url + route,
        method: 'PUT',
        header: {
          'Authorization': this.globalData.token,
          // 'content-type': 'multipart/form-data'
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: obj,
        success: (res) => {
          resolve(res)
        },
        fail: (res) => {
          reject(res)
        }
      })
    })
  },

  async getfavorites() {
    const res = await this.get('collections/0/', {})
    this.globalData.favorities = res.data
    return 0
  },

  async initlogin() {
    await new Promise((resolve, reject) => {
      wx.login({
        success: async (res) => {
          if (res.code) {
            const post = await this.post('login/', {
              'code': res.code
            })
            this.globalData.token = post.data.token
            await this.getfavorites();
            resolve()
          } else {
            console.log("登录失败" + res.errMsg)
            reject()
          }
        }
      })
    })
    return 0
  },

  async changeFav(isfav, docData) {
    try {
      let choose = await wx.showModal({
        title: `${isfav? '取消':''}收藏`,
        content: `请确认是否${isfav?'取消' :''}收藏（${docData.filename}）`,
      })
      if (choose.confirm && isfav) {
        let res = await this.delete(`collections/${docData.id}/`)
      } else if (choose.confirm) {
        let res = await this.post(`collections/${docData.id}/`)
      }else{
        return false
      }
      await this.getfavorites()
      return true
    } catch (error) {
      wx.showToast({
        title: `${isfav?'取消':''}收藏失败`,
        icon: 'error',
        duration: 1000,
        complete: () => {
          wx.hideToast()
        }
      })
      return false
    }
  },
  slideRightShow: function (that, param, px, opacity) {
    var select = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    });
    select.translateX(px).opacity(opacity).step()
    var json = '{"' + param + '":""}'
    json = JSON.parse(json);
    json[param] = select.export()
    that.setData(json)
  },
})